package com.dullwolf.controller;


import com.dullwolf.common.Constant;
import com.dullwolf.common.Result;
import com.dullwolf.utils.JsonUtils;
import com.jfinal.kit.HttpKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import io.jboot.utils.ArrayUtils;
import io.jboot.utils.StringUtils;
import io.jboot.web.controller.JbootController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class BaseController extends JbootController {



    /**
     * 成功返回
     */
    protected void SuccessResult(Object data) {
        Result result = new Result(Constant.SUCCESS_CODE, Constant.SUCCESS, data);
        renderJson(result);
    }

    /**
     * 失败返回
     */
    protected void FailedResult(Object data) {
        Result result = new Result(Constant.FAILED_CODE, Constant.FAILED, data);
        renderJson(result);
    }


    /**
     * 从前端传递过来的Json中读取指定Key的String值
     */
    protected String getJsonStringKey(String key) {
        String value = null;
        HttpServletRequest request = getRequest();
        String jsonString = HttpKit.readData(request);
        if (StringUtils.isNotBlank(jsonString)) {
            Map map = JsonUtils.getObjectByJson(jsonString, Map.class);
            if (ArrayUtils.isNotEmpty(map) && map.containsKey(key)) {
                value = map.get(key).toString();
            }
        }
        return value;
    }

    /**
     * 将前端传递过来的Json转换成对应的Model对象
     * @param t
     */
    protected <T extends Model> T jsonToModel(Class<T> t) {
        T model = null;
        HttpServletRequest request = getRequest();
        String jsonString = HttpKit.readData(request);
        if (StringUtils.isNotBlank(jsonString)) {
            try {
                Map map = JsonUtils.getObjectByJson(jsonString, Map.class);
                model = t.newInstance();
                model.put(map);
                model.put("_auto_copy_model_", true);
                //model.put(ModelCopier.MODEL_FROM_COPIER, true);
            } catch (Exception ignored) {
            }
        }
        return model;
    }

    protected <T extends Model> Map<String, Object> getAttrsMap(T t) {
        Map<String, Object> map = new HashMap<>();
        T model;
        if (StringUtils.isNotBlank(t)) {
            try {
                model = t;
                String[] attrNames = model._getAttrNames();
                Object[] objects = model._getAttrValues();
                for (int i = 0; i < attrNames.length; i++) {
                    map.put(attrNames[i], objects[i]);
                }
            } catch (Exception ignored) {
            }
        }
        Object convert = JsonUtils.convert(JsonUtils.getJsonString(map));
        return JsonUtils.getMapByObject(convert);

    }

    protected <T extends Model> List<Map<String, Object>> getAttrsList(List<T> t) {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map;
        T model;
        if (ArrayUtils.isNotEmpty(t)) {
            try {
                for (T aT : t) {
                    model = aT;
                    String[] attrNames = model._getAttrNames();
                    Object[] objects = model._getAttrValues();
                    map = new HashMap<>();
                    for (int i = 0; i < attrNames.length; i++) {
                        map.put(attrNames[i], objects[i]);
                    }
                    Object convert = JsonUtils.convert(JsonUtils.getJsonString(map));
                    list.add(JsonUtils.getMapByObject(convert));
                }
            } catch (Exception ignored) {
            }
        }
        return list;

    }


}
